<?php

class Application_Model_DbTable_Patient extends Zend_Db_Table_Abstract {

    protected $_name = 'patient';
    protected $_primary = "id_patient"; //clé primaire 
    protected $_sequence = true; // auto incrementé
    protected $_referenceMap = array(
        "Adresse" => array(
            "columns" => array("id_adresse"),
            "refTableClass" => "Application_Model_DbTable_Adresse",
            "refColumns" => array("id_adresse")));

    //
    //Noms de toutes les tables qui ont des relations avec la table Patient  
    //protected $_dependentTables = array("Adresse");

    public function getAllPatient() {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('p' => 'patient'), array('id_patient', 'titre', 'nom', 'prenom'))
                ->joinleft(array('a' => 'adresse'), ' p.id_adresse=a.id_adresse', array('adresse', 'ville', 'cpostal'))
                ->order('p.nom');

        //echo $select;

        /*
         * fonctionne bien
          $user = $this->fetchRow('id_patient = 1');
          $adresse = $user->findParentRow('Application_Model_DbTable_Adresse', 'Adresse');
          Zend_Debug::dump($adresse);
         */

        //$result = $this->fetchAll($select);
        //Zend_Debug::dump($result);
        //exit;

        return $this->fetchAll($select);
    }

    public function getPatientByName($name) {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('p' => 'patient'), array('id_patient', 'nom', 'prenom'))
                ->where('p.nom LIKE ' . $this->_db->quote($name . '%'))
                ->limit(10);

        // echo $select;
        $row = $this->fetchAll($select);

        if (!$row) {
            throw new Exception("Patient introuvable.");
        }

        //var_dump($row->toArray());
        //die;

        return $row->toArray();
    }

    public function getPatient($id) {

        // la verification de l'existence de id est faite dans le controller
        $id = (int) $id;
        $date_naissance = new Zend_Db_Expr("DATE_FORMAT(date_naissance,  '%d/%m/%Y') as date_naissance");

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('p' => 'patient'), array(
                    'id_patient', 'titre', 'nom', 'prenom', $date_naissance, 'numberSS', 'email'))
                ->joinleft(array('a' => 'adresse'), ' p.id_adresse=a.id_adresse') // array(champs) pour resteindre les champs
                ->where('p.id_patient = ?', $this->_db->quote($id, 'INTEGER'));

        //echo $select;
        $row = $this->fetchRow($select);

        if (!$row) {
            throw new Exception("Patient nr $id introuvable.");
        }
        return $row->toArray();
    }

    public function addPatient($data) {

        $patient = Cab_Utils::setUcfist($data['Patient'], array('titre', 'numberSS'));
        $adresse = Cab_Utils::setUcfist($data['Adresse'], array('cpostal', 'portable', 'tel'));
        $patient['id_patient'] = null;
        $adresse['id_adresse'] = null;
        $date = new Zend_Date($patient['date_naissance'], 'dd/MM/yyyy');
        $patient['date_naissance'] = $date->toString('yyyy-MM-dd');
        $date = new Zend_Date();
        $patient['date_patient'] = $date->toString('yyyy-MM-dd');

        //Zend_Debug::dump($patient);
        //exit;

        $adresseTable = new Application_Model_DbTable_Adresse();
        $new_id = $adresseTable->addAdresse($adresse);

        $patient['id_adresse'] = $new_id;
        $this->insert($patient);
    }

    public function editPatient($data) {

        //echo "<h2>editPatient</h2>";
        //var_dump($data);
        //
        // premiere lettre des mots en majuscule, champs a ne pas traiter
        $patient = Cab_Utils::setUcfist($data['Patient'], array('titre', 'id_patient', 'numberSS'));
        $adresse = Cab_Utils::setUcfist($data['Adresse'], array('cpostal', 'portable', 'tel'));

        $date = new Zend_Date($patient['date_naissance'], 'dd/MM/yyyy');
        $patient['date_naissance'] = $date->toString('yyyy-MM-dd');
        //var_dump($date);

        $adresseTable = new Application_Model_DbTable_Adresse();
        $adresseTable->editAdresse($adresse, $adresse['id_adresse']);

        //var_dump($patient);
        // exit;
        $this->update($patient, 'id_patient = ' . (int) $patient['id_patient']);
    }

    public function deletePatient($id) {
        $this->delete('id_patient =' . (int) $id);
    }

}
